// pages/discount/Discount.js
var Zan = require('../../dist/index.js');
let network = null;
let utils = null;
let api = null;
const app = getApp();
var start01 = 0;
var start02 = 0;
Page(Object.assign({}, Zan.TopTips, Zan.Toast,{

  /**
   * 页面的初始数据
   */
  data: {
    activeIndex:0,
    BrandCoupon:[],
    ClassCoupon: [],
    nodata: {
      msg: '暂无数据',
      notype: 1,
      imgurl: '/images/nodatas.png',
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    network = new app.NetWorkUtils();
    utils = new app.Utils();
    api = new app.Api();

    this.setData({
      imgSize: utils.windowSize().winWidth / 3 - 30
    })

    this.onPullDownRefresh();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
    if (this.data.activeIndex == 0){
      start01 = 0;
      this.setData({
        BrandCoupon:[]
      })
      network._post_page_json(this, api.getBrandList, start01, {}, Brandsuccess);
    }else{
      start02 = 0;
      this.setData({
        ClassCoupon:[]
      })
      network._post_page_json(this, api.getTenantType, start02, {}, Classsuccess);
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.activeIndex == 0) {
      if (this.data.BrandCoupon.length < this.data.recordsTotal) {
        start01 = start01 + 20;
        network._post_page_json(this, api.getBrandList, start01, {}, Brandsuccess);
      }
    } else {
      if (this.data.ClassCoupon.length < this.data.recordsTotal) {
        start02 = start02 + 20;
        network._post_page_json(this, api.getTenantType, start02, {}, Classsuccess);
      }
    }
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {
  
  // },
  /**
   * 点击tab
   */
  selected: function(e){
    let index = e.currentTarget.dataset.index;
    this.setData({
      activeIndex: index
    })
    if(index == 0){
      start01 = 0;
      this.setData({
        BrandCoupon: []
      })
      network._post_page_json(this, api.getBrandList, start01, {}, Brandsuccess);
    }else if(index == 1){
      start02 = 0;
      this.setData({
        ClassCoupon: []
      })
      network._post_page_json(this, api.getTenantType, start02, {}, Classsuccess);
    }

  },
  goCouponSearch: function(e){
    wx.navigateTo({
      url: '/pages/discount/SearchCoupon',
    })
  },
  goBrand: function(e){
    var id = e.currentTarget.dataset.id;
    var status = e.currentTarget.dataset.status;
    var name = e.currentTarget.dataset.name;
    //console.log(id);
    wx.navigateTo({
      url: '/pages/discount/BrandCoupon' + '?brandId=' + id + '&status=' + status + '&name=' + encodeURIComponent(name),
    })
  },
  goClass: function (e) {
    var id = e.currentTarget.dataset.id;
    var name = e.currentTarget.dataset.name;
    //console.log(status);
    wx.navigateTo({
      url: '/pages/discount/ClassCoupon' + '?id=' + id + '&name=' + encodeURIComponent(name),
    })
  }

}))
var Brandsuccess = function(that,res){
  if(res.data.data){
    that.setData({
      BrandCoupon: that.data.BrandCoupon.concat(res.data.data),
      recordsTotal: res.data.recordsTotal
    })
  }
}
var Classsuccess = function (that, res) {
  if (res.data.data) {
    that.setData({
      ClassCoupon: that.data.ClassCoupon.concat(res.data.data),
      recordsTotal: res.data.recordsTotal
    })
  }
}